
package com.scn.base.page;

import com.scn.base.constant.Constants;


/**
 * @author 
 *
 * 分页的类。该类通常用于在Form中获取分页信息。
 */
public class Pagination extends ValueObject {
	
	
    private final String currentTag = "current";                   //当前页
    private final String firstAction = "First";                    //第一页
    private final String nextAction = "Next";                      //下一页
    private final String previoAction = "Previo";                  //前一页
    private final String lastAction = "Last";                      //最后一页
    private final String gotoPageAction = "gotoPage";              //到第几页
    private String paginationActionTag = "First";                   //动作标志
    private int pageSelect = 0;                        //选择第几页
	private int start = 0;                             //开始位置
	private int end = 0;                               //结束位置
	private int count;                                 //一共多少条
	private int pageSize = Constants.PAGESIZE;    //每页多少条
	private int current =1;                            //当前第几页

    /**
     * 将Pagination对象转化为DivPageVO对象，DivPageVO只用于后台程序分页使用。
     * @return DivPageVO
     */
	public DivPageVO toDivPageVO() {
		DivPageVO divPageVO = new DivPageVO();
		divPageVO.setStartPos(this.getStart()); 
		divPageVO.setEndPos(this.getEnd());
		divPageVO.setPageSize(this.getPageSize());
		return divPageVO;
	}
    
    /**
     * 构造函数，初始化某些数据。
     * @param count 表示共有多少条记录。
     */
	public Pagination(int count) {
		this.count = count;
        this.paginationActionTag = "First";
        this.start = 0;
        this.end = 0;
        this.pageSize = Constants.PAGESIZE;
        this.current = 1;
	}
    
    /**
     * 获得分页从第几条记录开始。 
     * @return 第几条记录。
     */
	public int getStart() {

        if (getPaginationActionTag().equals(firstAction)) {
            firstPage();
        }
        else if (getPaginationActionTag().equals(nextAction)) {
            nextPage(getCurrent());
        }
        else if (getPaginationActionTag().equals(previoAction)) {
            previoPage(getCurrent());
        }
        else if (getPaginationActionTag().equals(lastAction)) {
            lastPage();
        }
        else if (getPaginationActionTag().equals(gotoPageAction)) {
			gotoPage(getPageSelect());
        }
        start = getPageSize() * (getCurrent() - 1);
     
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public void firstPage() {
		current = 1;
	}

	public void nextPage(int iCurrent) {
		current = iCurrent + 1;
		if (current * pageSize > count)
			lastPage();
	}

	public void previoPage(int iCurrent) {
		current = iCurrent - 1;
		if (current <= 0)
			firstPage();
	}

	public void lastPage() {
		if (count % pageSize == 0)
			current = count / pageSize;
		else
			current = count / pageSize + 1;
	}

	public void gotoPage(int pageSelect) {
		if (pageSelect <= 1)
			firstPage();
		else if (pageSelect * pageSize >= count)
			lastPage();
		else
			current = pageSelect;
	}

	public void setEnd(int end) {
		this.end = end;
	}

	public int getEnd() {
		if (pageSize * current > count)
			end = count;
		else
			end = pageSize * current;
		return end;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getCount() {
		return count;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPages() {
		if (count % pageSize == 0) {
			return count / pageSize;
		}
		else {
			return count / pageSize + 1;
		}
	}

	public int getPageSize() {
		return pageSize;
	}

	public boolean firstEnable() {
		return previoEnable();
	}

	public boolean lastEnable() {
		return nextEnable();
	}

	public boolean nextEnable() {
		return current * pageSize < count;
	}

	public boolean previoEnable() {
		return current > 1;
	}

	public void pagination(String paginationAction, String sCurrent) {
		int iCurrent = current;
		try {
			iCurrent = Integer.parseInt(sCurrent);
		}
		catch (Exception e) {
			iCurrent = 1;
		}
		if (paginationAction != null) {
			if (paginationAction.equals(firstAction))
				firstPage();
			if (paginationAction.equals(nextAction))
				nextPage(iCurrent);
			if (paginationAction.equals(previoAction))
				previoPage(iCurrent);
			if (paginationAction.equals(lastAction))
				lastPage();
			if (paginationAction.equals(gotoPageAction))
				gotoPage(iCurrent);
		}
	}

	public int getCurrent() {
		return current;
	}
	public void setCurrent(int current) {
		this.current = current;
	}
	/**
	 * @return
	 */
	public String getPaginationActionTag() {
		return paginationActionTag;
	}

	/**
	 * @param string
	 */
	public void setPaginationActionTag(String string) {
		paginationActionTag = string;
	}

	/**
	 * @return
	 */
	public int getPageSelect() {
		return pageSelect;
	}

	/**
	 * @param i
	 */
	public void setPageSelect(int i) {
		pageSelect = i;
	}

}
